package com.morgan.test;

import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class CTTimeUnit {
    
    private final Map<String, Data> m = new TreeMap<String, Data>();
    private static final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
    private static final Lock r = rwl.readLock();
    private static final Lock w = rwl.writeLock();
    
    public Data get(String key) {
        r.lock();
        try { return m.get(key); }
        finally { r.unlock(); }
    }
    public Object[] allKeys() {
        r.lock();
        try { return m.keySet().toArray(); }
        finally { r.unlock(); }
    }
    public Data put(String key, Data value) {
        w.lock();
        try { return m.put(key, value); }
        finally { w.unlock(); }
    }
    public void clear() {
        w.lock();
        try { m.clear(); }
        finally { w.unlock(); }
    }
    
    public static void main(String[] args) throws InterruptedException {
        
        TimeUnit tu = TimeUnit.DAYS;

        System.out.println(tu.toDays(1));
        System.out.println(tu.toHours(1));
        System.out.println(tu.toMinutes(1));

        if (r.tryLock(50L, TimeUnit.NANOSECONDS)) {
            System.out.println("Hello0");
        }

        if (r.tryLock(50L, TimeUnit.MILLISECONDS)) {
            System.out.println("Hello1");
        }

        if (r.tryLock(50L, TimeUnit.SECONDS)) {
            System.out.println("Hello2");
        }

    }

}
